package com.wish.bio;

import com.wish.common.MyUtils;

import java.io.*;
import java.net.Socket;

/**
 * @author tantexian
 * @since 2016/6/24
 */
public class SimpleClient {

    public static void main(String[] args) {
        final int port = args.length >= 1 ? Integer.parseInt(args[0]) : MyUtils.getDefaultPort();

        Socket socket = null;
        BufferedReader bufferedReader = null;
        PrintWriter printWriter = null;

        try {
            socket = new Socket(MyUtils.getDefaultServerAddr(), port);

            bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));

            printWriter.println("what's you name");
            printWriter.flush();
            String robotResp = bufferedReader.readLine();
            System.out.println("The answer from robot is: " + robotResp);

            printWriter.println("what's the time");
            printWriter.flush();
            robotResp = bufferedReader.readLine();
            System.out.println("The answer from robot is: " + robotResp);


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (printWriter != null) {
                    printWriter.close();
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
}
